package register.Model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import register.Model.entity.Students;
import register.utils.JDBCUtils;

public class RegisterDaoImpl implements RegisterDao {

    public boolean addStudents(Students students) throws SQLException {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            // 1.获取连接
            conn = JDBCUtils.getConnection();
            String sql = "INSERT INTO user (`username`,`password`,`postbox`,`phoneNumber`,`name`,`direction`,`phase`) VALUES(?,?,?,?,?,?,?)";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, students.getUsername());
            pstm.setString(2, students.getPassword());
            pstm.setString(3, students.getPostbox());

            pstm.setString(4, students.getPhoneNumber());

            pstm.setString(5, students.getName());
            pstm.setString(6, students.getDirection());
            pstm.setString(7, students.getPhase());

            int i = pstm.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pstm.close();
            JDBCUtils.freeConnection();
        }
        return true;
    }

    public String register(String username, String password, String repassword, int phoneNumber, String email) {
        return null;
    }

    public Integer countUserName(String username) throws SQLException {
        Integer count = 0;
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            // 1.获取连接
            conn = JDBCUtils.getConnection();
            String sql = "select count(1)count from user where username =?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, username);

            ResultSet resultSet = pstm.executeQuery();
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pstm.close();
            JDBCUtils.freeConnection();
        }
        return count;
    }

    public Integer countO(String name, String direction, String phase) throws SQLException {
        Integer count = 0;
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            // 1.获取连接
            conn = JDBCUtils.getConnection();
            String sql = "select count(1)count from information where name =? and direction=? and phase=?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, name);
            pstm.setString(2, direction);
            pstm.setString(3, phase);

            ResultSet resultSet = pstm.executeQuery();
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pstm.close();
            JDBCUtils.freeConnection();
        }
        return count;
    }
}
